﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using br.com.SistemaChamados.Model.Common;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.Model;

namespace br.com.SistemaChamados.DAO.Common
{
    public class UserDAO
    {
        private static string SQL_INSERT_USER = " INSERT INTO TBL_USER (TBL_USERNAME, TBL_USERLOGIN, TBL_USERPASSWORD, TBL_SECTORID) VALUES (@name, @eMail, @password, @sectorid) ";
        private static string SQL_UPDATE_USER = " UPDATE TBL_USER SET TBL_USERNAME = @name, TBL_USERLOGIN = @eMail, TBL_USERPASSWORD = @password, TBL_SECTORID=@sectorid WHERE TBL_USERID = @id ";
        private static string SQL_DELETE_USER = " DELETE FROM TBL_USER WHERE TBL_USERID = @id ";
        private static string SQL_GET_USERS = " SELECT * FROM TBL_USER ";
        private static string SQL_GET_USER = " SELECT * FROM TBL_USER WHERE TBL_USERID = @id";

        public static User getUser(User user)
        {
            IDbConnection conn = null;
            IDbCommand command = null;
            IDataReader reader = null;
            User userReturn = new User();
            Sector sector = new Sector();
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_GET_USER, conn);
                command.Parameters.Add(Mapped.Parameter("@id", user.Id));
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                reader.Read();
                userReturn.Id = Convert.ToInt32(reader["TBL_USERID"].ToString());
                userReturn.Name = Convert.ToString(reader["TBL_USERNAME"].ToString());
                userReturn.EMail = Convert.ToString(reader["TBL_USERLOGIN"].ToString());
                userReturn.Password = Convert.ToString(reader["TBL_USERPASSWORD"].ToString());
                sector.Id=Convert.ToInt32(reader["TBL_SECTORID"].ToString());
                userReturn.Sector = sector; 
            }
            catch (Exception ex)
            {
                userReturn = null;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return userReturn;
        }

        public static List<User> getUsers()
        {
            List<User> list = new List<User>();
            IDbConnection conn = null;
            IDbCommand command = null;
            IDataReader reader = null;
            User userReturn = new User();
            Sector sector = new Sector();
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_GET_USERS, conn);
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    userReturn = new User();
                    userReturn.Id = Convert.ToInt32(reader["TBL_USERID"].ToString());
                    userReturn.Name = Convert.ToString(reader["TBL_USERNAME"].ToString());
                    userReturn.EMail = Convert.ToString(reader["TBL_USERLOGIN"].ToString());
                    userReturn.Password = Convert.ToString(reader["TBL_USERPASSWORD"].ToString());
                    sector.Id = Convert.ToInt32(reader["TBL_SECTORID"].ToString());
                    userReturn.Sector = sector; 
                    list.Add(userReturn);
                    userReturn = null;
                }
            }
            catch (Exception ex)
            {
                list = null;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
            return list;
        }

        public static EnumGenericState InsertUser(User user)
        {

            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            IDbConnection conn = null;
            IDbCommand command = null;

            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_INSERT_USER, conn);
                command.Parameters.Add(Mapped.Parameter("@name", user.Name));
                command.Parameters.Add(Mapped.Parameter("@eMail", user.EMail));
                command.Parameters.Add(Mapped.Parameter("@password", user.Password));
                command.Parameters.Add(Mapped.Parameter("@sectorid", user.Sector.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumGenericState.GENERIC_STATE_SUCCESS;
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return stateTransaction;

        }

        public static EnumGenericState UpdateUser(User user)
        {

            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            IDbConnection conn = null;
            IDbCommand command = null;

            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_UPDATE_USER, conn);
                command.Parameters.Add(Mapped.Parameter("@name", user.Name));
                command.Parameters.Add(Mapped.Parameter("@eMail", user.EMail));
                command.Parameters.Add(Mapped.Parameter("@password", user.Password));
                command.Parameters.Add(Mapped.Parameter("@id", user.Id));
                command.Parameters.Add(Mapped.Parameter("@sectorid", user.Sector.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumGenericState.GENERIC_STATE_SUCCESS;
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return stateTransaction;

        }

        public static EnumGenericState DeleteUser(User user)
        {
            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            IDbConnection conn = null;
            IDbCommand command = null;

            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_DELETE_USER, conn);
                command.Parameters.Add(Mapped.Parameter("@id", user.Id));
                command.ExecuteNonQuery();
                stateTransaction = EnumGenericState.GENERIC_STATE_SUCCESS;
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return stateTransaction;

        }
    }
}